define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pro/user/index',
                    add_url: 'pro/user/add',
                    edit_url: 'pro/user/edit',
                    // del_url: 'pro/user/del',
                    multi_url: 'pro/user/multi',
                    table: 'gzns_user_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate:false},


                        {field: 'custom_status', title: __('Custom_status'), searchList: {"0":__('Custom_status 0'),"1":__('Custom_status 1')}, formatter: Table.api.formatter.status, visible:false},
                        {field: 'nick_name', title: __('Nick_name'), formatter: Controller.api.formatter.name},
                        {field: 'avatar', title: __('Avatar'), formatter: Table.api.formatter.image,operate: false},
                        {field: 'real_name', title: __('Real_name'), operate:'LIKE'},
                        {field: 'phone', title: __('Phone'), operate:'LIKE'},
                        // {field: 'identity_code', title: __('Identity_code')},
                        {field: 'company_name', title: __('Company.company_name'), operate:'LIKE'},
                        // {field: 'post', title: __('职务'), operate:'LIKE'},
                        // {field: 'company.company_principal', title: __('Company.company_principal'), operate:'LIKE'},
                        // {field: 'vip.level.vip_type', title: __('Company.Level.vip_type'), searchList: {"1":__('Vip_type 1'),"2":__('Vip_type 2')}, formatter: Table.api.formatter.status},
                        {field: 'vip.level.name', title: __('Company.Level.name')},
                        // {field: 'vip.level.discount', title: __('Company.Level.discount'), operate:'BETWEEN', operate:false},
                        // {field: 'vip.end_time', title: __('Vip.end_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime, operate:false},

                        {field: 'sex', title: __('Sex'), searchList: {"0":__('Sex 0'),"1":__('Sex 1')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __('Status'), searchList: {"0":__('Status 0'),"1":__('Status 1')}, formatter: Table.api.formatter.toggle},
                        {field: 'account.user_name', title: __('Account.user_name'), formatter: Controller.api.formatter.user_name, operate:'LIKE'},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {
                            field: 'buttons',
                            title: __('会员申请历史'),
                            table: table,
                            events: Table.api.events.operate,
                            operate: false,
                            buttons: [
                                {
                                    name: '会员申请历史',
                                    text: __('会员申请历史'),
                                    title: __('会员申请历史'),
                                    classname: 'btn btn-xs btn-warning btn-addtabs',
                                    icon: 'fa fa-list',
                                    url: 'pro/vipapply/index?type=sub&user_id={id}'
                                }
                            ],
                            formatter: Table.api.formatter.buttons
                        },
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
            $('#c-vip_level_id').data("params", function (obj) {
                return {custom: {vip_type: $('#c-vip_type').val()}};
            });

            $('#c-vip_type').on('change',function () {
                $('#c-vip_level_id').selectPageClear();
            })
            $('[name="row[delete_vip]"]').on('change',function () {
                console.log($(this).val())
                toggleDeleteVip($(this).val())
            })
            console.log($('#c-vip_level_id'))
            toggleDeleteVip($('[name="row[delete_vip]"]:checked').val())
            function toggleDeleteVip(flag) {
                console.log($('.delete-vip [name^=row]'))
                if (flag == 0) {
                    $('form[role=form]').data('validator').options.ignore = '.delete-vip [name^=row]';
                    // $('.delete-vip [name^=row]').attr('novalidate','true');
                    // $('.delete-vip [name^=row]').attr('disabled','true');
                    $('.delete-vip').hide()
                }
                if (flag == 1) {
                    $('form[role=form]').data('validator').options.ignore = false;

                    // $('.delete-vip [name^=row]').removeAttr('novalidate');
                    // $('.delete-vip [name^=row]').removeAttr('disabled');
                    $('.delete-vip').show()
                }
            }
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {
                name: function (value, row, index) {
                    if (!value)
                        return '游客';

                    return value;
                },
                user_name: function (value, row, index) {
                    if (!value)
                        return '未设置帐号';

                    return value;
                },
            }
        },

    };
    return Controller;
});
